package com.xsh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.mapper.MenuMapper;
import com.xsh.pojo.Menu;
import com.xsh.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashSet;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public LinkedHashSet<Menu> findAllMenu(String username) {
        LinkedHashSet<Menu> menus = menuMapper.selectByConditions(username, 0, null);
        for (Menu menu : menus) {
            LinkedHashSet<Menu> secondMenus = findMenuTwo(username, menu.getMenuId());
            menu.setList(secondMenus);
        }
        return menus;
    }

    @Override
    public LinkedHashSet<Menu> findMenuTwo(String username, Integer parentId) {
        return menuMapper.selectByConditions(username, 1, parentId);
    }

    @Override
    public List<Menu> findAll() {
        return menuMapper.selectAll(null);
    }

    @Override
    public PageInfo<Menu> findByPageInfo(String order, Integer limit, Integer offset) {
        PageHelper.startPage((offset / limit) + 1, limit);
        PageInfo<Menu> pageInfo = new PageInfo<>(menuMapper.selectAll(order));
        return pageInfo;
    }
}
